package com.dx.financehy.utils;

import cn.hutool.core.util.BooleanUtil;

import java.util.Random;

/**
 * 一些不好归类的常用工具类
 * Created by jinjin on 2020-10-01.
 */
public class MyUtil {

    private static String[] NUMBERS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private static Random RANDOM = new Random();

    /**
     * 生成length位随机数值字符串
     *
     * @param length
     * @return
     */
    public static String randomCode(int length) {
        StringBuilder builder = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            builder.append(NUMBERS[RANDOM.nextInt(NUMBERS.length)]);
        }

        return builder.toString();
    }

    public static String generatePassword(int length) {
        String result = getRandomPassword(length);
        if (BooleanUtil.toBoolean(PasswordValidation.checkPwd(result))) {
            return result;
        }
        return generatePassword(length);
    }

    public static String getRandomPassword(int length) {
        char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~!@#$%^&*.?".toCharArray();
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int x = 0; x < length; ++x) {
            sb.append(charr[r.nextInt(charr.length)]);
        }
        return sb.toString();
    }

}
